/**
 * 
 */
package com.wskj.library.employer.controller;

import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.wskj.library.pojo.Employer;
import com.wskj.library.service.EmployerService;
import com.wskj.library.service.impl.EmployerServiceImpl;

/**
 * @author 胡洋
 * @describe 
 * @Date 2018年3月27日下午3:59:59
 * @version 1.0
 */
@MultipartConfig
@WebServlet("/employer/importExcel.do")
public class ImportExcelEmployer extends HttpServlet {
	private  EmployerService emService = new EmployerServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.getRequestDispatcher("/WEB-INF/page/employer/importExcel.jsp").forward(req, resp);
		
	}
	
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Employer> employers=new ArrayList<Employer>();
		Part part = req.getPart("employer");		
		
		
		
		
		try (InputStream is = part.getInputStream();
			Workbook workbook = WorkbookFactory.create(is);){
			
			Sheet sheet = workbook.getSheetAt(0);
			//循环遍历 excel 表格的数据 .....
			for(int i=1;i<=sheet.getLastRowNum();i++) {
				Row row = sheet.getRow(i);
				
				if(row.getCell(0) == null) {
					break;
				}
				
				String  e_number=(String) getCellValue(row.getCell(1));
				String e_name=(String) getCellValue(row.getCell(2));
				int e_rid=(int) Double.parseDouble(row.getCell(3).toString());
				String  account=(String) getCellValue(row.getCell(4));
				//手机号码格式化下.....
				DecimalFormat df = new DecimalFormat("#");				
				String e_phone= df.format(getCellValue(row.getCell(5)));
				String email=(String) getCellValue(row.getCell(6));
				Date birthday=(Date)getCellValue(row.getCell(7));
				Date joinday=(Date) getCellValue(row.getCell(8));
				
				Employer em=new Employer(e_number, account, e_name, e_rid, e_phone, email, birthday, joinday);
				
				employers.add(em);				
			}	
			
		} catch (EncryptedDocumentException e) {		
			e.printStackTrace();
		} catch (InvalidFormatException e) {			
			e.printStackTrace();
		}
		
		
		 boolean result=emService.saveEmployerList(employers);
		
		 resp.sendRedirect(req.getContextPath()+"/employer/list.do");
		
		
		
	}
	
	/**
	 * 根据单元格的数据类型 获取单元格的值 ......
	 * @param cell 单元格
	 * @return
	 */
	private Object getCellValue(Cell cell) {
       Object obj=null;
		switch (cell.getCellTypeEnum()) {
		case STRING:
			obj=cell.getRichStringCellValue().getString();
			break;
		case NUMERIC:
			if (DateUtil.isCellDateFormatted(cell)) {
				obj=cell.getDateCellValue();
			} else {
				obj=cell.getNumericCellValue();
			}
			break;
		case BOOLEAN:
			obj=cell.getBooleanCellValue();
			break;
		case FORMULA:
			obj=cell.getCellFormula();
			break;
		case BLANK:
			break;			
		default:
			break;
		}
		return obj;
	}
}
